package csu.softwaremetric.service;
import java.io.*;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.utils.Constants;
import com.google.gson.JsonObject;
import org.springframework.stereotype.Service;

import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.utils.JsonUtils;


import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.aigc.generation.models.QwenParam;
import com.alibaba.dashscope.common.Message;
import com.alibaba.dashscope.common.MessageManager;
import com.alibaba.dashscope.common.Role;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;


@Service
public class ChatService {

    public String getCKMetricChat(String question)
    {
        Constants.apiKey="XXXXX";
        Generation gen = new Generation();
        MessageManager msgManager = new MessageManager(10);
        String prompt = "你擅长进行CK软件度量，现在我将告诉你我对一些Java类的CK度量结果，格式为Json，包括: CBO - Coupling between Objects, " +
                "DIT - Depth of Inheritance Tree, LCOM - Lack of Cohesion of Methods, NOC - Number of Children, " +
                "RFC - Response For Class, WMC - Weighted Methods for Class 这6个指标。每个类的名称在Json中的key为name，" +
                "请为我分析具体每一个类的各项指标。比如CBO和LCOM的值是否偏高，如果偏高有什么不良影响或潜在风险。请用中文回答";
        Message systemMsg =
                Message.builder().role(Role.SYSTEM.getValue()).content(prompt).build();
        Message userMsg = Message.builder().role(Role.USER.getValue()).content(question).build();
        msgManager.add(systemMsg);
        msgManager.add(userMsg);
        QwenParam param =
                QwenParam.builder().model(Generation.Models.QWEN_TURBO).messages(msgManager.get())
                        .resultFormat(QwenParam.ResultFormat.MESSAGE)
                        .build();
        GenerationResult result = null;
        try {
            result = gen.call(param);
        } catch (NoApiKeyException | InputRequiredException e) {
            e.printStackTrace();
        }
        JsonObject jsonObject = JsonUtils.toJsonObject(result);
        return jsonObject.getAsJsonObject("output").getAsJsonArray("choices").get(0).
                getAsJsonObject().getAsJsonObject("message").get("content").getAsString();
    }

    public String getFunctionMetricChat(String question)
    {
        Constants.apiKey="sk-815b7b5e5c6c46aea0d573d8b7f127e0";
        Generation gen = new Generation();
        MessageManager msgManager = new MessageManager(10);
        String prompt = "你擅长进行功能点软件度量，现在我将告诉你我对一些Java类的功能点度量结果，格式为Json，包括: UFC - Unadjusted Function Count, " +
                "VAF - Value Adjustment Factor, FP - Function Point  EISimple, EOSimple（简单外部输出的数量）,"+
        "EQSimple, EIFSimple, ILFSimple, EIAve, EOAve, EQAve, EIFAve, ILFAve, EIComplex, EOComplex, EQComplex," +
        "EIFComplex, ILFComplex这几个指标这几个指标。" +
                "请为我分析具体每一个类的各项指标。比如UFC的值是否偏高，如果偏高有什么不良影响或潜在风险。请用中文回答";
        Message systemMsg =
                Message.builder().role(Role.SYSTEM.getValue()).content(prompt).build();
        Message userMsg = Message.builder().role(Role.USER.getValue()).content(question).build();
        msgManager.add(systemMsg);
        msgManager.add(userMsg);
        QwenParam param =
                QwenParam.builder().model(Generation.Models.QWEN_TURBO).messages(msgManager.get())
                        .resultFormat(QwenParam.ResultFormat.MESSAGE)
                        .build();
        GenerationResult result = null;
        try {
            result = gen.call(param);
        } catch (NoApiKeyException | InputRequiredException e) {
            e.printStackTrace();
        }
        JsonObject jsonObject = JsonUtils.toJsonObject(result);
        return jsonObject.getAsJsonObject("output").getAsJsonArray("choices").get(0).
                getAsJsonObject().getAsJsonObject("message").get("content").getAsString();
    }

    public String getUseCaseMetricChat(String question)
    {
        Constants.apiKey="sk-815b7b5e5c6c46aea0d573d8b7f127e0";
        Generation gen = new Generation();
        MessageManager msgManager = new MessageManager(10);
        String prompt = "你擅长进行用例点软件度量，现在我将告诉你我对一些用例图的用例点度量结果，格式为Json，包括: " +
                "UUCP - Unadjusted UseCase Points, TCF - Technical Complexity Factor, " +
                "EF - Environmental Factor, UPC - Adjusted UseCase Points 这4个指标。每个类的名称在Json中的key为name，" +
                "请为我分析具体每一个类的各项指标。比如UAW和UUCW的值是否偏高，如果偏高有什么不良影响或潜在风险,这些值偏高或偏低代表着什么。请用中文回答";
        Message systemMsg =
                Message.builder().role(Role.SYSTEM.getValue()).content(prompt).build();
        Message userMsg = Message.builder().role(Role.USER.getValue()).content(question).build();
        msgManager.add(systemMsg);
        msgManager.add(userMsg);
        QwenParam param =
                QwenParam.builder().model(Generation.Models.QWEN_TURBO).messages(msgManager.get())
                        .resultFormat(QwenParam.ResultFormat.MESSAGE)
                        .build();
        GenerationResult result = null;
        try {
            result = gen.call(param);
        } catch (NoApiKeyException | InputRequiredException e) {
            e.printStackTrace();
        }
        JsonObject jsonObject = JsonUtils.toJsonObject(result);
        return jsonObject.getAsJsonObject("output").getAsJsonArray("choices").get(0).
                getAsJsonObject().getAsJsonObject("message").get("content").getAsString();
    }

    public String getMcCabeChat(String question, String xmlName)
    {
        Constants.apiKey="sk-815b7b5e5c6c46aea0d573d8b7f127e0";
        Generation gen = new Generation();
        MessageManager msgManager = new MessageManager(10);
        String prompt = "你擅长进行McCabe软件度量，现在我将告诉你一个XML格式的控制流图，并告诉你这个控制流图的环复杂度。请对这个控制流图" +
                "进行分析，结合换复杂度的大小，给出该控制流图的优点、缺点，以及如何改进。请用中文回答";
        Message systemMsg =
                Message.builder().role(Role.SYSTEM.getValue()).content(prompt).build();

        StringBuilder xmlContent = new StringBuilder();
        try (BufferedReader bufferedReader = new BufferedReader(new FileReader("upload/XML/" + xmlName))) {
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                xmlContent.append(line);
            }
        }
        catch (IOException e) {
            return "文件不存在";
        }

        Message userMsg = Message.builder().role(Role.USER.getValue()).content(xmlContent+ "该控制流图的换复杂度为" + question).build();
        msgManager.add(systemMsg);
        msgManager.add(userMsg);
        QwenParam param =
                QwenParam.builder().model(Generation.Models.QWEN_TURBO).messages(msgManager.get())
                        .resultFormat(QwenParam.ResultFormat.MESSAGE)
                        .build();
        GenerationResult result = null;
        try {
            result = gen.call(param);
        } catch (NoApiKeyException | InputRequiredException e) {
            e.printStackTrace();
        }
        JsonObject jsonObject = JsonUtils.toJsonObject(result);
        return jsonObject.getAsJsonObject("output").getAsJsonArray("choices").get(0).
                getAsJsonObject().getAsJsonObject("message").get("content").getAsString();
    }

}
